package com.plm.frame.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.plm.frame.service.FavoriteService;
import com.plm.frame.service.ProductService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.plm.frame.core.Constants;
import com.plm.frame.model.Favorite;
import com.plm.frame.model.Product;
import com.plm.frame.model.User;

@Controller
public class FavoriteAction extends BaseAction{
	
	@Resource
	private FavoriteService favoriteService;
	@Resource
	private ProductService productService;
	
	@RequestMapping("/addFavorite")
	public String addFavorite(Map<String,List> m,Product product){
		Favorite favorite = new Favorite();
		favorite.setUsername(((User) getSession().getAttribute(Constants.LOGIN_USER)).getUsername());
		favorite.setProduct(productService.selectProductAsId(product));
		favoriteService.addFavorite(favorite);
		return "redirect:showProduct?id="+product.getId();
	}
	
	@RequestMapping("/listAddFavorite")
	public String listAddFavorite(Map<String,List> m,Product product){
		Favorite favorite = new Favorite();
		favorite.setUsername(((User) getSession().getAttribute(Constants.LOGIN_USER)).getUsername());
		favorite.setProduct(productService.selectProductAsId(product));
		favoriteService.addFavorite(favorite);
		return "redirect:products";
	}
	
	@RequestMapping("/favorite")
	public String favorite(Map<String,Object> m){
		Favorite favorite = new Favorite();
		favorite.setUsername(((User) getSession().getAttribute(Constants.LOGIN_USER)).getUsername());
		m.put("favorite", favoriteService.selectFavorite(favorite));
		if(favoriteService.selectFavorite(favorite).size() == 0){
			m.put("wufavorite", "收藏为空");
		}
		return "favorite";
	}
	
	@RequestMapping("/listDeleteFavorite")
	public String listDeleteFavorite(Map<String,List> m,Product product){
		Favorite favorite = new Favorite();
		favorite.setCid(product.getId());
		favorite.setUsername(((User) getSession().getAttribute(Constants.LOGIN_USER)).getUsername());
		favoriteService.deleteFavorite(favorite);
		return "redirect:products";
	}
	
	@RequestMapping("/deleteFavorite")
	public String deleteFavorite(Map<String,List> m,Product product){
		Favorite favorite = new Favorite();
		favorite.setCid(product.getId());
		favorite.setUsername(((User) getSession().getAttribute(Constants.LOGIN_USER)).getUsername());
		favoriteService.deleteFavorite(favorite);
		return "redirect:showProduct?id="+product.getId();
	}
	
	@RequestMapping("/fdeleteFavorite")
	public String fdeleteFavorite(Map<String,List> m,Favorite favorite){
		favoriteService.deleteFavorite(favorite);
		return "redirect:favorite";
	}
}
